nanopyx.methods.drift_alignment.estimator_table

  1import os
  2import numpy as np
  3from importlib import metadata
  4from datetime import datetime
  5
  6
  7class DriftEstimatorTable(object):
  8    """
  9    Class used to store DriftAlignment parameters as a dictionary.
 10    Parameters can be changes individually by setting the corresponding params key value to desired parameter
 11    """
 12    def __init__(self):
 13        self.params = {}
 14        self.params["lib_version"] = metadata.version("nanopyx")
 15        self.params["date"] = datetime.today()
 16        self.params["apply"] = False
 17        self.params["do_batch"] = False
 18        self.params["ref_option"] = 1 # 0 if it is to use first frame, 1 if uses the previous frame
 19        self.params["time_averaging"] = 1
 20        self.params["max_expected_drift"] = 0
 21        self.params["normalize"] = True
 22        self.params["shift_calc_method"] = "Max Fitting"
 23        self.params["use_roi"] = False
 24        self.params["roi"] = None
 25        self.params["show_ccm"] = True  # used for napari
 26        self.params["show_drift_plot"] = True  # used for napari
 27        self.params["show_drift_table"] = True  # used for napari
 28        self.params["comments"] = None
 29
 30        self.drift_table = None
 31
 32    def set_params(self, **kwargs):
 33        """
 34        Method used to set the parameters of drift alignment using keyword arguments.
 35        :param kwargs: same as self.params.keys()
 36        """
 37        for key, value in kwargs.items():
 38            self.params[key] = value
 39
 40    def set_comments(self, comment_string: str):
 41        """
 42        Method used to set comments for drift alignment operation
 43        :param comment_string: str, comment text to be added
 44        """
 45        self.params["comments"] = comment_string
 46
 47    def export_npy(self, path: str = None):
 48        """
 49        Method used to export drift table as a npy file.
 50        :param path: Path to export drift table as npy
 51        """
 52        tmp = []
 53        for key in self.params.keys():
 54            tmp.append((key, self.params[key]))
 55        tmp.append(self.drift_table)
 56        if path is None:
 57            path = input("Please provide a filepath to export drift table as npy") + "_drift_table.npy"
 58        else:
 59            path = os.path.join(path, "_drift_table.npy")
 60
 61        np.save(path, np.array(tmp, dtype=object))
 62
 63    def import_npy(self, path: str = None):
 64        """
 65        Method used to import drift table as a npy file.
 66        :param path: str, Path to drift table saved as a npy file
 67        """
 68        if path is None:
 69            path = input("Please provide a filepath to import drift table")
 70
 71        tmp = np.load(path, allow_pickle=True)
 72
 73        for i in range(tmp.shape[0]-1):
 74            key, value = tmp[i]
 75            self.params[key] = value
 76        self.drift_table = tmp[tmp.shape[0]-1]
 77
 78    def export_csv(self, path: str = None):
 79        """
 80        Method used to export drift table as a csv file.
 81        :param path: str, Path to export drift table as csv
 82        """
 83        if path is None:
 84            path = input("Please provide a filepath to export drift table as csv") + "_drift_table.csv"
 85        else:
 86            path = os.path.join(path, "_drift_table.csv")
 87
 88        txt = ""
 89        for key in self.params.keys():
 90            txt += key + ";" + str(self.params[key]) + "\n"
 91        txt += "Drift Table\n"
 92        txt += "XY;X;Y\n"
 93        for i in range(self.drift_table.shape[0]):
 94            txt += str(self.drift_table[i][0]) + ";" + str(self.drift_table[i][1]) + ";" + str(self.drift_table[i][2]) + "\n"
 95
 96        open(path, "w").writelines(txt)
 97
 98    def import_csv(self, path: str = None):
 99        """
100        Method used to import drift table from a csv file
101        :param path: str, path to import drift table as csv
102        """
103        if path is None:
104            path = input("Please provide a filepath to import drift table")
105
106        tmp = open(path, "r").readlines()
107
108        count = 0
109        for line in tmp:
110            if line == "Drift Table\n":
111                break
112            else:
113                count += 1
114            param_split = line.split(";")
115            key = param_split[0]
116            value = param_split[1].split("\n")[0]
117            if value == "True":
118                value = True
119            elif value == "False":
120                value = False
121            elif value == "None":
122                value = None
123            self.params[key] = value
124
125        if self.params["roi"] is not None:
126            roi_str_list = self.params["roi"][1:-1].split(", ")
127            self.params["roi"] = tuple([int(coord) for coord in roi_str_list])
128
129        drift_table = []
130
131        for row in tmp[count+2:]:
132            row_split = row.split(";")
133            drift_xy = float(row_split[0])
134            drift_x = float(row_split[1])
135            drift_y = float(row_split[2])
136            drift_table.append([drift_xy, drift_x, drift_y])
137
138        self.drift_table = np.array(drift_table)
class DriftEstimatorTable:
  8class DriftEstimatorTable(object):
  9    """
 10    Class used to store DriftAlignment parameters as a dictionary.
 11    Parameters can be changes individually by setting the corresponding params key value to desired parameter
 12    """
 13    def __init__(self):
 14        self.params = {}
 15        self.params["lib_version"] = metadata.version("nanopyx")
 16        self.params["date"] = datetime.today()
 17        self.params["apply"] = False
 18        self.params["do_batch"] = False
 19        self.params["ref_option"] = 1 # 0 if it is to use first frame, 1 if uses the previous frame
 20        self.params["time_averaging"] = 1
 21        self.params["max_expected_drift"] = 0
 22        self.params["normalize"] = True
 23        self.params["shift_calc_method"] = "Max Fitting"
 24        self.params["use_roi"] = False
 25        self.params["roi"] = None
 26        self.params["show_ccm"] = True  # used for napari
 27        self.params["show_drift_plot"] = True  # used for napari
 28        self.params["show_drift_table"] = True  # used for napari
 29        self.params["comments"] = None
 30
 31        self.drift_table = None
 32
 33    def set_params(self, **kwargs):
 34        """
 35        Method used to set the parameters of drift alignment using keyword arguments.
 36        :param kwargs: same as self.params.keys()
 37        """
 38        for key, value in kwargs.items():
 39            self.params[key] = value
 40
 41    def set_comments(self, comment_string: str):
 42        """
 43        Method used to set comments for drift alignment operation
 44        :param comment_string: str, comment text to be added
 45        """
 46        self.params["comments"] = comment_string
 47
 48    def export_npy(self, path: str = None):
 49        """
 50        Method used to export drift table as a npy file.
 51        :param path: Path to export drift table as npy
 52        """
 53        tmp = []
 54        for key in self.params.keys():
 55            tmp.append((key, self.params[key]))
 56        tmp.append(self.drift_table)
 57        if path is None:
 58            path = input("Please provide a filepath to export drift table as npy") + "_drift_table.npy"
 59        else:
 60            path = os.path.join(path, "_drift_table.npy")
 61
 62        np.save(path, np.array(tmp, dtype=object))
 63
 64    def import_npy(self, path: str = None):
 65        """
 66        Method used to import drift table as a npy file.
 67        :param path: str, Path to drift table saved as a npy file
 68        """
 69        if path is None:
 70            path = input("Please provide a filepath to import drift table")
 71
 72        tmp = np.load(path, allow_pickle=True)
 73
 74        for i in range(tmp.shape[0]-1):
 75            key, value = tmp[i]
 76            self.params[key] = value
 77        self.drift_table = tmp[tmp.shape[0]-1]
 78
 79    def export_csv(self, path: str = None):
 80        """
 81        Method used to export drift table as a csv file.
 82        :param path: str, Path to export drift table as csv
 83        """
 84        if path is None:
 85            path = input("Please provide a filepath to export drift table as csv") + "_drift_table.csv"
 86        else:
 87            path = os.path.join(path, "_drift_table.csv")
 88
 89        txt = ""
 90        for key in self.params.keys():
 91            txt += key + ";" + str(self.params[key]) + "\n"
 92        txt += "Drift Table\n"
 93        txt += "XY;X;Y\n"
 94        for i in range(self.drift_table.shape[0]):
 95            txt += str(self.drift_table[i][0]) + ";" + str(self.drift_table[i][1]) + ";" + str(self.drift_table[i][2]) + "\n"
 96
 97        open(path, "w").writelines(txt)
 98
 99    def import_csv(self, path: str = None):
100        """
101        Method used to import drift table from a csv file
102        :param path: str, path to import drift table as csv
103        """
104        if path is None:
105            path = input("Please provide a filepath to import drift table")
106
107        tmp = open(path, "r").readlines()
108
109        count = 0
110        for line in tmp:
111            if line == "Drift Table\n":
112                break
113            else:
114                count += 1
115            param_split = line.split(";")
116            key = param_split[0]
117            value = param_split[1].split("\n")[0]
118            if value == "True":
119                value = True
120            elif value == "False":
121                value = False
122            elif value == "None":
123                value = None
124            self.params[key] = value
125
126        if self.params["roi"] is not None:
127            roi_str_list = self.params["roi"][1:-1].split(", ")
128            self.params["roi"] = tuple([int(coord) for coord in roi_str_list])
129
130        drift_table = []
131
132        for row in tmp[count+2:]:
133            row_split = row.split(";")
134            drift_xy = float(row_split[0])
135            drift_x = float(row_split[1])
136            drift_y = float(row_split[2])
137            drift_table.append([drift_xy, drift_x, drift_y])
138
139        self.drift_table = np.array(drift_table)

Class used to store DriftAlignment parameters as a dictionary. Parameters can be changes individually by setting the corresponding params key value to desired parameter

params
drift_table
def set_params(self, **kwargs):
33    def set_params(self, **kwargs):
34        """
35        Method used to set the parameters of drift alignment using keyword arguments.
36        :param kwargs: same as self.params.keys()
37        """
38        for key, value in kwargs.items():
39            self.params[key] = value

Method used to set the parameters of drift alignment using keyword arguments.

Parameters
  • kwargs: same as self.params.keys()
def set_comments(self, comment_string: str):
41    def set_comments(self, comment_string: str):
42        """
43        Method used to set comments for drift alignment operation
44        :param comment_string: str, comment text to be added
45        """
46        self.params["comments"] = comment_string

Method used to set comments for drift alignment operation

Parameters
  • comment_string: str, comment text to be added
def export_npy(self, path: str = None):
48    def export_npy(self, path: str = None):
49        """
50        Method used to export drift table as a npy file.
51        :param path: Path to export drift table as npy
52        """
53        tmp = []
54        for key in self.params.keys():
55            tmp.append((key, self.params[key]))
56        tmp.append(self.drift_table)
57        if path is None:
58            path = input("Please provide a filepath to export drift table as npy") + "_drift_table.npy"
59        else:
60            path = os.path.join(path, "_drift_table.npy")
61
62        np.save(path, np.array(tmp, dtype=object))

Method used to export drift table as a npy file.

Parameters
  • path: Path to export drift table as npy
def import_npy(self, path: str = None):
64    def import_npy(self, path: str = None):
65        """
66        Method used to import drift table as a npy file.
67        :param path: str, Path to drift table saved as a npy file
68        """
69        if path is None:
70            path = input("Please provide a filepath to import drift table")
71
72        tmp = np.load(path, allow_pickle=True)
73
74        for i in range(tmp.shape[0]-1):
75            key, value = tmp[i]
76            self.params[key] = value
77        self.drift_table = tmp[tmp.shape[0]-1]

Method used to import drift table as a npy file.

Parameters
  • path: str, Path to drift table saved as a npy file
def export_csv(self, path: str = None):
79    def export_csv(self, path: str = None):
80        """
81        Method used to export drift table as a csv file.
82        :param path: str, Path to export drift table as csv
83        """
84        if path is None:
85            path = input("Please provide a filepath to export drift table as csv") + "_drift_table.csv"
86        else:
87            path = os.path.join(path, "_drift_table.csv")
88
89        txt = ""
90        for key in self.params.keys():
91            txt += key + ";" + str(self.params[key]) + "\n"
92        txt += "Drift Table\n"
93        txt += "XY;X;Y\n"
94        for i in range(self.drift_table.shape[0]):
95            txt += str(self.drift_table[i][0]) + ";" + str(self.drift_table[i][1]) + ";" + str(self.drift_table[i][2]) + "\n"
96
97        open(path, "w").writelines(txt)

Method used to export drift table as a csv file.

Parameters
  • path: str, Path to export drift table as csv
def import_csv(self, path: str = None):
 99    def import_csv(self, path: str = None):
100        """
101        Method used to import drift table from a csv file
102        :param path: str, path to import drift table as csv
103        """
104        if path is None:
105            path = input("Please provide a filepath to import drift table")
106
107        tmp = open(path, "r").readlines()
108
109        count = 0
110        for line in tmp:
111            if line == "Drift Table\n":
112                break
113            else:
114                count += 1
115            param_split = line.split(";")
116            key = param_split[0]
117            value = param_split[1].split("\n")[0]
118            if value == "True":
119                value = True
120            elif value == "False":
121                value = False
122            elif value == "None":
123                value = None
124            self.params[key] = value
125
126        if self.params["roi"] is not None:
127            roi_str_list = self.params["roi"][1:-1].split(", ")
128            self.params["roi"] = tuple([int(coord) for coord in roi_str_list])
129
130        drift_table = []
131
132        for row in tmp[count+2:]:
133            row_split = row.split(";")
134            drift_xy = float(row_split[0])
135            drift_x = float(row_split[1])
136            drift_y = float(row_split[2])
137            drift_table.append([drift_xy, drift_x, drift_y])
138
139        self.drift_table = np.array(drift_table)

Method used to import drift table from a csv file

Parameters
  • path: str, path to import drift table as csv